#!/usr/bin/env python2.4
__license__ = "GNU Lesser General Public License, Ver.2"
__author__ = "Grigory Bakunov <bobuk@justos.org>"

import os
import sys

_mcfg_ = None

def Config():
    global _mcfg_
    if _mcfg_ == None:
        _mcfg_ = wmConfig()
    return _mcfg_

class wmConfig:
    def __init__(self, fname='templates/config.cfg'):
        self.fname = fname
        self.cfg = {}
        self.order = []
        self.load()

    def load(self):
        for x in open(self.fname).readlines():
            # #set c_key = 'value'
            [name, line] = x[5: -2].split(' = "')
            self.cfg[name] = line
            self.order.append(name)

    def save(self):
        _fl = open(self.fname, 'w')
        for name in self.order:
            _fl.write('#set %s = "%s"\n' % (name, self.cfg[name]))
        _fl.close()

    def update(self, mdict):
        for name in self.order:
            if mdict.has_key(name):
                self.cfg[name] = mdict[name]
        
            
